//
//  DDExchangeTokenMask.m
//  MyClouds
//
//  Created by sarah doo on 13-8-11.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDExchangeTokenMask.h"
#import "NSString+md5.h"
#import "DDCommentDic.h"

//#import <winbase.h>
#include <stdlib.h> /*用到了srand函数，所以要有这个头文件*/

@implementation DDExchangeTokenMask

@synthesize token,curToken;

//使用收到的数据，生成一个新的ExchangeTokenMask对象，用于接收数据
- (id)initWithData:(NSData *)data {
    self = [super init];
    if (self) {
        self.entity = [[NSMutableData alloc]init];
        [entity appendData:data];
    }
    return self;
}

//使用一个生成的TokenMask，创建一个新的ExchangeTokenMask，用于发送数据
- (id)initWithTokenMask:(NSString*)tokenStr
{
    self = [super init];
    
    if(self) {
        [self setCmd:3];
        int cmdLength = HEADER_LENGTH + TOKEN_MASK_LENGTH * 2;
        [self setLength:cmdLength];
        tokenStr = [self generateRandomTokenMask];
        NSData *localData = [tokenStr dataUsingEncoding:NSUnicodeStringEncoding];
        NSRange range;
        range.location = 2;
        range.length = [localData length] - 2;
        localData = [localData subdataWithRange:range];
        Byte *testByte = (Byte *)[localData bytes];
        [self setToken:localData];
        int crc = [self crcs:entity withLength:[entity length]];
        [self setCrc:crc];
    }
    return self;
}

- (void) setToken:(NSData *)_token
{
    //token = [self sortData:_token];
    
    NSRange range;
    
    range.location = 20;
    range.length = 64 * 2;
    Byte* entityBytes = (Byte*)[_token bytes];
    [entity replaceBytesInRange:range withBytes:entityBytes];
    entityBytes = (Byte*)[entity bytes];
}


- (NSData *)myHDRData {
    DDHdrCloud *hdrCloud = [[DDHdrCloud alloc] init];
    hdrCloud.cmd = CloudCmd_ExchangeTokenMask;
    hdrCloud.signature = CLOUD_SIGNATURE ;
    hdrCloud.version = CLOUD_VERSION_1_0;
    hdrCloud.length = 20;
    NSData *cloudHDRData = [hdrCloud hdrData];
    [hdrCloud release];
    return cloudHDRData;
}

- (NSString *)generateToken:(NSArray *)tokenString {
    NSString *string = [tokenString componentsJoinedByString:@""];
    NSString *final = [string md5HexDigest];
    return final;
}

//接受tockenMask并拼接并加密

- (NSString *)generateRandomTokenMask {
    srand(1000);
    NSMutableString *str = [NSMutableString stringWithCapacity:TOKEN_MASK_LENGTH];
    for (int i = 0; i < 63; i++) {
        int x = (rand() % 26) + 'A';
        char y = x;
        [str appendFormat:@"%c",y];
    }
    [str appendString:@"\0"];
    NSString *result = [[str copy]autorelease];

    [self setCurToken:result];
    return result ;
}


- (NSString *)getTokenMask
{
    NSRange range;
    range.location = HEADER_LENGTH;
    range.length = TOKEN_MASK_LENGTH * 2;
    NSData* tokenMaskData = [entity subdataWithRange:range];
    Byte headBytes[10];
    headBytes[0] = 255;
    headBytes[1] = 254;

    NSMutableData *reusltData = [[NSMutableData alloc]initWithBytes:headBytes length:2];
    [reusltData appendBytes:[tokenMaskData bytes]  length:[tokenMaskData length]];

    NSLog(@"--------------------%@",tokenMaskData);
    NSLog(@"--------------------%@",reusltData);
    NSString* tokenMaskStr = [[NSString alloc]initWithData:reusltData encoding:NSUnicodeStringEncoding];
    return tokenMaskStr;
}


@end
